package com.itheima.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.mapper.EmpExprMapper;
import com.itheima.mapper.EmpMapper;
import com.itheima.pojo.*;
import com.itheima.service.DeptService;
import com.itheima.service.EmpService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.time.LocalDateTime;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

@Service
public class EmpServiceImpl extends ServiceImpl <EmpMapper, Emp> implements EmpService {
    @Autowired
    private DeptService deptService;
    @Autowired
    private EmpExprMapper empExprMapper;

    @Override
    public PageResult<Emp> empPage(EmpQueryParam param) {
        //调用插件
        Page<Emp> page = new Page<>(param.getPage(), param.getPageSize());
        //按照时间倒序排序
        page.addOrder(OrderItem.desc("update_time"));
        //调用Iservice的方法进行条件判断,获取条件,最后接上条件，不能返回值后再调用
        lambdaQuery().like(param.getName() != null, Emp::getName, param.getName()).eq(param.getGender() != null,Emp::getGender, param.getGender()).between(param.getBegin() != null && param.getEnd() != null, Emp::getEntryDate, param.getBegin(), param.getEnd()).page( page);
    //调用page方法,执行分页查询

        //获取分页数据集合
        List<Emp> list = page.getRecords();
        if(!CollectionUtils.isEmpty( list)){
            //将所有的分页的数据集合的部门ID获取出来放到新的集合里面
            List<Integer> listMap = list.stream().map(Emp::getDeptId).collect(Collectors.toList());
            //根据部门ID查询部门名称
            List<Dept> deptList = deptService.listByIds(listMap);
            //对分页数据进行遍历
           if (!CollectionUtils.isEmpty(deptList)){
                deptList.forEach(dept -> {
                    list.forEach(emp -> {
                        if (emp.getDeptId()!=null && emp.getDeptId().equals(dept.getId())){
                            //设置部门名称
                            emp.setDeptName(dept.getName());
                        }
                    });
                });
           }

            
        }
        long pages = page.getTotal();
        return new PageResult<>(pages, list);
    }

    @Override
    public void saveExperience(Emp emp) {
        //保存员工基本信息
        emp.setUpdateTime(LocalDateTime.now());
        emp.setCreateTime(LocalDateTime.now());
        save(emp);
        //保存员工工作经历信息
        List<EmpExpr> exprList = emp.getExprList();
        if (!CollectionUtils.isEmpty(exprList)){
            exprList.forEach(expr -> {
                expr.setEmpId(emp.getId());
            });
            empExprMapper.insert(exprList);
        }
    }
    @Transactional
    @Override
    public void updateEmp(Emp emp) {
        //修改员工基本信息
        emp.setUpdateTime(LocalDateTime.now());
        updateById(emp);
        //删除员工工作经历信息
        LambdaQueryWrapper<EmpExpr> lqw = new LambdaQueryWrapper<>();
        lqw.eq(EmpExpr::getEmpId, emp.getId());
         empExprMapper.delete(lqw);
         //新增员工工作经历信息
        List<EmpExpr> exprList = emp.getExprList();
        if (!CollectionUtils.isEmpty(exprList)){
            exprList.forEach(expr -> {
                expr.setEmpId(emp.getId());
            });
            empExprMapper.insert(exprList);
        }

    }

    @Override
    public Emp getByEmpId(Integer id) {
        //查询员工基本信息
        Emp emp = getById(id);
        //查询员工工作经历信息
        LambdaQueryWrapper<EmpExpr> lqw = new LambdaQueryWrapper<>();
        lqw.eq(EmpExpr::getEmpId, id);
        List<EmpExpr> exprList = empExprMapper.selectList(lqw);
        emp.setExprList(exprList);
        return emp;
    }

    @Override
    public void removeEmps(List<Integer> ids) {
        //删除员工基本信息
        removeByIds(ids);
        //删除员工工作经历信息
        LambdaQueryWrapper<EmpExpr> lqw = new LambdaQueryWrapper<>();
        lqw.in(EmpExpr::getEmpId, ids);
        empExprMapper.delete(lqw);
    }


}
